package com.whw.fy.controller;

import com.alibaba.fastjson.JSON;
import com.whw.fy.entity.*;
import com.whw.fy.service.PlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Api(tags = "计划模块")
@RestController
@RequestMapping("/api/plan")
public class PlanController {
    @Resource
    private PlanService planService;

//    @ApiOperation(value = "添加计划信息")
//    @PostMapping("/one")
//    public Message insert(Plan plan) throws ParseException {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        if (plan.getPlanTimeToString()!=null){
//            plan.setPlanTime(simpleDateFormat.parse(plan.getPlanTimeToString()));
//        }
//        if(plan.getPlanExplaintimeToString() != null){
//            plan.setPlanExplaintime(simpleDateFormat.parse(plan.getPlanExplaintimeToString()));
//        }
//        return planService.insertSelective(plan);
//    }

    @ApiOperation(value = "添加计划信息传List<PlanSummarizeTable>，List<Area>")
    @PostMapping("/one")
    public Message insert(String areaList, String planSummarizeTableList,Plan plan){
        List<PlanSummarizeTable> planSummarizeTables = JSON.parseArray(planSummarizeTableList,PlanSummarizeTable.class);
        List<Area> areas = JSON.parseArray(areaList,Area.class);
        return planService.insertSelectives(plan,areas,planSummarizeTables);
    }

    @ApiOperation(value = "删除计划")
    @DeleteMapping("/")
    public Message delete(Integer planId) {
        return planService.deleteByPrimaryKey(planId);
    }

    @ApiOperation(value = "修改计划传List<PlanSummarizeTable>，List<Area>")
    @PutMapping("/")
    public Message update(String areaList, String planSummarizeTableList,Plan plan) {
        List<PlanSummarizeTable> planSummarizeTables = JSON.parseArray(planSummarizeTableList,PlanSummarizeTable.class);
        List<Area> areas = JSON.parseArray(areaList,Area.class);
        return planService.updateByPrimaryKeys(plan,areas,planSummarizeTables);
    }

    @ApiOperation(value = "获取计划")
    @GetMapping("/one")
    public Message selectByPrimaryKey(Integer planId){
        return planService.selectByPrimaryKey(planId);
    }

    @ApiOperation(value = "获取所有计划")
    @GetMapping("/all")
    public Message selectAll(){
        return planService.selectAll();
    }

    @ApiOperation(value = "根据学生id获得计划")
    @GetMapping("/studentId")
    public Message selectByPlanStudentId(int studentId){
        return planService.selectByPlanStudentId(studentId);
    }



}
